package com.mark.ipc;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Messenger;
import android.os.RemoteException;
import android.view.View;
import android.widget.Toast;

import com.mark.ipc.entity.Message;

public class MainActivity extends AppCompatActivity {

    private IConnectionService connectionServiceProxy;
    private IMessageService messageServiceProxy;
    private IServiceManager serviceManagerProxy;
    private Messenger messengerProxy;

    private Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull android.os.Message msg) {
            super.handleMessage(msg);
            Bundle bundle = msg.getData();
            bundle.setClassLoader(Message.class.getClassLoader());
            final Message message = bundle.getParcelable("message");
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(MainActivity.this, message.getContent(), Toast.LENGTH_SHORT).show();
                }
            },3000);

        }
    };

    private Messenger clientMessenger = new Messenger(mHandler);


    private MessageReceiveListener messageReceiveListener = new MessageReceiveListener.Stub() {
        @Override
        public void onReceiveMessage(final Message message) throws RemoteException {
            new Handler(Looper.getMainLooper()).post(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(MainActivity.this,
                            message.getContent(), Toast.LENGTH_SHORT).show();
                }
            });
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        Intent intent = new Intent(this, RemoteService.class);
        bindService(intent, new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                try {
                    serviceManagerProxy = IServiceManager.Stub.asInterface(service);
                    connectionServiceProxy = IConnectionService.Stub.asInterface(serviceManagerProxy
                            .getService(IConnectionService.class.getSimpleName()));
                    messageServiceProxy = IMessageService.Stub.asInterface(serviceManagerProxy
                            .getService(IMessageService.class.getSimpleName()));
                    messengerProxy = new Messenger(serviceManagerProxy
                            .getService(Messenger.class.getSimpleName()));
                } catch (RemoteException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onServiceDisconnected(ComponentName name) {

            }
        }, Context.BIND_AUTO_CREATE);
    }

    public void connecting(View view) {
        try {
            connectionServiceProxy.connect();
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    public void disConnecting(View view) {
        try {
            connectionServiceProxy.disconnect();
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    public void isConnected(View view) {
        try {
            boolean isConnected = connectionServiceProxy.isConnected();
            Toast.makeText(this, String.valueOf(isConnected), Toast.LENGTH_SHORT).show();
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    public void messageUnregister(View view) {
        try {
            messageServiceProxy.unRegisterMessageReceiveListener(messageReceiveListener);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    public void messageRegister(View view) {
        try {
            messageServiceProxy.registerMessageReceiveListener(messageReceiveListener);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    public void messageSend(View view) {
        try {
            Message message = new Message();
            message.setContent("this message from main");
            messageServiceProxy.sendMessage(message);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    public void messengerConnect(View view) {
        try {
            Message message = new Message();
            message.setContent("this message from main by massenger");
            android.os.Message data = new android.os.Message();
            data.replyTo = clientMessenger;
            Bundle bundle = new Bundle();
            bundle.putParcelable("message", message);
            data.setData(bundle);
            messengerProxy.send(data);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }
}
